package ${(projectConfig.projectPackageName)!}.base;

import cn.aezo.utils.base.ExceptionU;
import cn.aezo.utils.base.ValidU;
import ${(projectConfig.projectPackageName)!}.common.config.rawbean.CustomObjectMapper;
import ${(projectConfig.projectPackageName)!}.entity.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

@Component
public class BaseController {
    private static Logger logger = LoggerFactory.getLogger(BaseController.class);

    private static CustomObjectMapper customObjectMapper;

    public BaseController() {}

    @Autowired
    public BaseController(CustomObjectMapper customObjectMapper) {
        BaseController.customObjectMapper = customObjectMapper;
    }

    public static Long getUserId(HttpServletRequest request) {
        User user = getSessionUser(request);
        if(user == null) return null;

        Long id = user.getId();
        if(ValidU.isEmpty(id)) return null;

        return id;
    }

    public static User getSessionUser(HttpServletRequest request) {
        return (User) request.getSession().getAttribute(BaseKeys.SessionUserInfo);
    }

    public static void writeJson(HttpServletResponse response, Object object) {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");

        PrintWriter writer = null;
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if(writer != null) {
            String str;
            if(object instanceof String) {
                str = (String) object;
            } else {
                try {
                    str = customObjectMapper.writeValueAsString(object);
                } catch (JsonProcessingException e) {
                    String msg = "json convert error, data:" + object;
                    logger.error(msg, e);
                    throw new ExceptionU.DataConvertException(msg);
                }
            }

            writer.write(str);
            writer.flush();
            writer.close();
        }
    }

    public static void writeSuccess(HttpServletResponse response, String message) {
        Result result = Result.success(message);
        writeJson(response, result);
    }

    public static void writeSuccess(HttpServletResponse response, String message, Object data) {
        Result result = Result.success(message, data);
        writeJson(response, result);
    }

    public static void writeError(HttpServletResponse response, String message) {
        Result result = Result.failure(message);
        writeJson(response, result);
    }

    public static void writeError(HttpServletResponse response, String message, Object data) {
        Result result = Result.failure(message, data);
        writeJson(response, result);
    }
}
